package cn.elwy.kit.file.service;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import cn.elwy.common.util.io.FileUtil;

public class BackupRecoveryFile {
  public static String ext = ".back";
  private static Scanner in;

  public static void main(String[] args) throws IOException {
    in = new Scanner(System.in);

    System.out.println("1.备份文件    2.恢复文件");
    boolean inputState = true;

    File srcFile = null;
    File descFile = null;
    int exeType = in.nextInt();
    if (exeType == 1) {
      do {
        System.out.println("请输入原始目录或文件");
        String srcPath = in.next();
        if (srcPath == null || srcPath.equals("") || !new File(srcPath).exists()) {
          inputState = false;
        } else {
          inputState = true;
          srcFile = new File(srcPath);
        }
      } while (!inputState);
      do {
        System.out.println("请输入目标目录或文件");
        String descPath = in.next();
        if (descPath == null || descPath.equals("")) {
          inputState = false;
        } else {
          inputState = true;
          descFile = new File(descPath);
        }
      } while (!inputState);

      copyFiles(srcFile, descFile, "");
      System.out.println("备份成功！");
    } else if (exeType == 2) {
      do {
        System.out.println("请输入原始目录或文件");
        String srcPath = in.next();
        if (srcPath == null || srcPath.equals("") || !new File(srcPath).exists()) {
          inputState = false;
        } else {
          inputState = true;
          srcFile = new File(srcPath);
        }
      } while (!inputState);
      renameTo(srcFile);
      System.out.println("重命名成功！");
    } else {
      return;
    }
  }

  public static boolean copyFiles(File srcFile, File destDir, String fileName) throws IOException {
    if (!srcFile.exists()) { // 文件不存在
      return false;
    }
    if (!destDir.exists()) {
      destDir.mkdirs();
    }
    if (!destDir.isDirectory()) {
      return false;
    }
    boolean result = true;
    if (srcFile.isFile()) {
      String tempName = fileName + ext;
      return FileUtil.copyFile(srcFile, destDir, tempName, true);
    } else {
      destDir = new File(destDir, fileName);
      destDir.mkdirs();
      File[] children = srcFile.listFiles();
      if (children == null) {
        return true;
      }
      for (File file : children) {
        if (file.isFile()) {
          String tempName = file.getName() + ext;
          result = FileUtil.copyFile(file, destDir, tempName, true);
        } else {
          result = copyFiles(file, destDir, file.getName());
        }
        if (!result) {
          return false;
        }
      }
    }
    return result;
  }

  public static boolean renameTo(File srcFile) throws IOException {
    if (!srcFile.exists()) { // 文件不存在
      return false;
    }
    boolean result = true;
    if (srcFile.isFile()) {
      String name = srcFile.getName();
      String tempName = name.substring(0, name.length() - ext.length());

      File destFile = new File(srcFile.getParentFile(), tempName);
      if (destFile.exists()) {
        destFile.delete();
      }
      return srcFile.renameTo(destFile);
    } else {
      File[] children = srcFile.listFiles();
      if (children == null) {
        return true;
      }
      for (File file : children) {
        if (file.isFile()) {
          String name = file.getName();
          String tempName = name.substring(0, name.length() - ext.length());

          File destFile = new File(file.getParentFile(), tempName);
          if (destFile.exists()) {
            destFile.delete();
          }
          result = file.renameTo(destFile);
        } else {
          result = renameTo(file);
        }
        if (!result) {
          return false;
        }
      }
    }
    return result;
  }

}
